<?php

namespace app\admin\controller;

use app\admin\traits\Backend;

class Position extends \app\admin\controller\Backend
{
    protected $model = null;

    protected $needSelectCompany = true;
    protected $beforeActionList = [
        'getCurrentCompany',
    ];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Position();
    }


    public function index()
    {
        list($where, $sort, $order, $offset, $limit) = $this->buildparams();
        $where = [];
        if ($this->request->param('query')) {
            $where ['name'] = ['like', '%' . $this->request->param('query') . '%'];
        }
        $where['company_id'] = $this->getCurrentCompany()->id;
        $list = $this->model
            ->where($where)
            ->order($sort, $order)
            ->paginate($limit);
        $this->assign('list', $list);
        $this->assign('params', [
            'query' => $this->request->param('query'),
        ]);
        return $this->fetch('index');
    }

    public function dc()
    {
        $company = $this->company;
        $company_id = $company->id;
        $where['company_id'] = $company_id;
        $list = $this->model
            ->where($where)
            ->order($sort, $order)
            ->paginate($limit);
        foreach ($list as $k => $v) {
            if ($v['is_approver'] == 2) {
                $v['approver_name'] = '是';
            } else {
                $v['approver_name'] = '否';
            }
            $list[$k] = $v;
        }
        vendor('phpexcel/PHPExcel');
        //3.实例化PHPExcel类
        $objPHPExcel = new \PHPExcel();
        //exit($objPHPExcel);
        //4.激活当前的sheet表
        $objPHPExcel->setActiveSheetIndex(0);
        //5.设置表格头（即excel表格的第一行）
        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', '名称')
            ->setCellValue('B1', '描述')
            ->setCellValue('C1', '审批人');


        //设置A列水平居中
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        //设置单元格宽度
        //6.循环刚取出来的数组，将数据逐一添加到excel表格。
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(20);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('D')->setWidth(20);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('E')->setWidth(20);


        for ($i = 0; $i < count($list); $i++) {
            $objPHPExcel->getActiveSheet()->setCellValue('A' . ($i + 2), $list[$i]['name']);         //ID
            $objPHPExcel->getActiveSheet()->setCellValue('B' . ($i + 2), $list[$i]['describe']);     //标签码
            $objPHPExcel->getActiveSheet()->setCellValue('C' . ($i + 2), $list[$i]['approver_name']);//防伪码

        }
        //7.设置保存的Excel表格名称
        $filename = 'yz' . date('ymd', time()) . '.xls';
        //8.设置当前激活的sheet表格名称；
        $objPHPExcel->getActiveSheet()->setTitle('cs');
        //9.设置浏览器窗口下载表格
        header("Content-Type: application/force-download");
        header("Content-Type: application/octet-stream");
        header("Content-Type: application/download");
        header('Content-Disposition:inline;filename="' . $filename . '"');
        //生成excel文件
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        //下载文件在浏览器窗口
        $objWriter->save('php://output');
        exit;


    }

    public function add()
    {
        $data = Request()->post();

        $data['company_id'] = $this->getCurrentCompany()->id;
        $data['create_time'] = time();

        $res = db('position')->insert($data);
        if ($res) {
            return $this->success('成功', url('index'));
        } else {
            return $this->error('失败', url('index'));
        }
    }
}